package com.apes.fn.scm.retailOrder.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.Name;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import com.apes.framework.util.DateUtil;
import com.apes.hr.area.model.DeptArea;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.party.model.PartyRole;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.lang.reflect.Field;
import java.util.Date;
import java.util.Map;

/**
 * @program: apes-server
 * @description
 * @author: czs
 * @create: 2021-11-24 10:58
 **/
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = CarWasherReport.CONST_TABLE_NAME)
public class CarWasherReport extends Model {
    public static final String CONST_TABLE_NAME = "car_washer_report";

    @Id
    @Column(name = "id")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "洗美报表编码", padding = 8)
    private String id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "dept_area_id", referencedColumnName = "id")
    @Comment("门店区域")
    private DeptArea deptArea;

    @Comment("部门")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "dept_id", referencedColumnName = "id", nullable = false)
    private Dept dept;

    @Comment("门店等级")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "store_levels", referencedColumnName = "id")
    private Enumeration storeLevels;

    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "statistic_date")
    @Comment("统计日期")
    private Date statisticDate;


    @Column(name = "common_count", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("普通洗车-台次")
    private double commonCount = 0;

    @Column(name = "common_amount", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("普通洗车-金额")
    private double commonAmount = 0;

    @Column(name = "common_amount_n", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("普通洗车-金额占比")
    private double commonAmountN = 0;

    @Column(name = "deep_count", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("深度洗车-台次")
    private double deepCount = 0;

    @Column(name = "deep_amount", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("深度洗车-金额")
    private double deepAmount = 0;

//    @Column(name = "deep_amount_n", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
//    @Comment("深度洗车-金额占比")
//    private double deepAmountN = 0;

    @Column(name = "all_count", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("洗车合计-台次")
    private double allCount = 0;

    @Column(name = "all_amount", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("洗车合计-金额")
    private double allAmount = 0;

    @Column(name = "wax_amount", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("打蜡护理-金额")
    private double waxAmount = 0;

    @Column(name = "wax_amount_n", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("打蜡护理-金额占比")
    private double waxAmountN = 0;

    @Column(name = "crystal_count", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("镀晶护理-台次")
    private double crystalCount = 0;

    @Column(name = "crystal_amount", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("镀晶护理-金额")
    private double crystalAmount = 0;

    @Column(name = "crystal_amount_n", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("镀晶护理-金额占比")
    private double crystalAmountN = 0;

    @Column(name = "dry_count", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("室内干洗-台次")
    private double dryCount = 0;

    @Column(name = "dry_amount", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("室内干洗-金额")
    private double dryAmount = 0;

    @Column(name = "dry_amount_n", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("室内干洗-金额占比")
    private double dryAmountN = 0;

    @Column(name = "engine_count", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("发动机仓护理-台次")
    private double engineCount = 0;

    @Column(name = "engine_amount", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("发动机仓护理-金额")
    private double engineAmount = 0;

    @Column(name = "engine_amount_n", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("发动机仓护理-金额占比")
    private double engineAmountN = 0;

    @Column(name = "catalyst_count", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("光触媒-台次")
    private double catalystCount = 0;

    @Column(name = "catalyst_amount", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("光触媒-金额")
    private double catalystAmount = 0;

    @Column(name = "catalyst_amount_n", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("光触媒-金额占比")
    private double catalystAmountN = 0;

    @Column(name = "air_count", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("室内空气治理-台次")
    private double airCount = 0;

    @Column(name = "air_amount", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("室内空气治理-金额")
    private double airAmount = 0;

    @Column(name = "air_amount_n", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("室内空气治理-金额占比")
    private double airAmountN = 0;

    @Column(name = "other_beauty_amount", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("其他美容-金额")
    private double otherBeautyAmount = 0;

    @Column(name = "other_beauty_amount_n", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("其他美容-金额占比")
    private double otherBeautyAmountN = 0;

    @Column(name = "beauty_count", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("美容合计-台次")
    private double beautyCount = 0;

    @Column(name = "beauty_amount", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("美容合计-金额")
    private double beautyAmount = 0;


    @Column(name = "beauty_card_count", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("美容年卡-个数")
    private double beautyCardCount = 0;

    @Column(name = "beauty_card_amount", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("美容年卡-金额")
    private double beautyCardAmount = 0;


    @Comment("主管名称")
    @Column(name = "supervisor_name", length = 30)
    private String supervisorName;

    @Comment("主管人员")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "supervisor_id", referencedColumnName = "id")
    private PartyRole supervisorId;

    @Column(name = "supervisor_amount", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("主管个人总体服务业绩-金额")
    private double supervisorAmount = 0;


    @Column(name = "washer_beauty_amount", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("洗美合计业绩")
    private double washerBeautyAmount = 0;

    @Column(name = "serve_count", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("美容暖心服务数量")
    private double serveCount = 0;

    @Column(name = "serve_amount", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("美容暖心服务合计")
    private double serveAmount = 0;


    public void setField(String fieldName, Map<String,Double> param){

        try {
            Field declaredField = null;
            if (param.get("count")!=null){
                if (fieldName.equals("wax"))return;
                declaredField = getClass().getDeclaredField(fieldName+"Count");
                declaredField.setAccessible(true);
                Double i = (Double) declaredField.get(this);
                declaredField.set(this,i.intValue() + param.get("count").intValue());
            }
            if (param.get("amount")!=null){
                declaredField = getClass().getDeclaredField(fieldName+"Amount");
                declaredField.setAccessible(true);
                declaredField.set(this,declaredField.getDouble(this)+param.get("amount"));
            }
            if (param.get("amountN")!=null){
                declaredField = getClass().getDeclaredField(fieldName+"AmountN");
                declaredField.setAccessible(true);
                declaredField.set(this,declaredField.getDouble(this)+param.get("AmountN"));
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void sumAmountN(){
        allAmount = commonAmount+deepAmount;
        allCount=deepCount+commonCount;
        if (allAmount!=0)commonAmountN = commonAmount/allAmount;
        if (beautyAmount!=0)waxAmountN = waxAmount/beautyAmount;
        if (beautyAmount!=0)crystalAmountN = crystalAmount/beautyAmount;
        if (beautyAmount!=0)dryAmountN = dryAmount/beautyAmount;
        if (beautyAmount!=0)engineAmountN = engineAmount/beautyAmount;
        if (beautyAmount!=0)catalystAmountN = catalystAmount/beautyAmount;
        if (beautyAmount!=0)airAmountN = airAmount/beautyAmount;
        otherBeautyAmount = beautyAmount-airAmount-catalystAmount-engineAmount-dryAmount-crystalAmount-waxAmount;
        if (beautyAmount!=0)otherBeautyAmountN = otherBeautyAmount/beautyAmount;
        washerBeautyAmount = allAmount+beautyAmount+beautyCardAmount;

    }

}
